using System.Windows;
using Microsoft.Practices.CompositeUI.Commands;

namespace SCSFContrib.CompositeUI.WPF.CommandAdapters
{
	/// <summary>
	/// Implements a <see cref="CommandAdapter"/> that adapts between CAB <see cref="Command"/>s and WPF <see cref="UIElement"/>s.
	/// </summary>
	public class UIElementCommandAdapter : EventCommandAdapter<UIElement>
	{
		/// <summary>
		/// Constructs an instance of <c>UIElementCommandAdapter</c>.
		/// </summary>
		public UIElementCommandAdapter()
		{
		}

		/// <summary>
		/// Constructs an instance of <c>UIElementCommandAdapter</c> with the specified information.
		/// </summary>
		/// <param name="invoker">
		/// The invoker that is to be adapted.
		/// </param>
		/// <param name="eventName">
		/// The event the adapter will listen to on the invoker.
		/// </param>
		public UIElementCommandAdapter(UIElement invoker, string eventName)
			: base(invoker, eventName)
		{
		}

		/// <summary>
		/// Handles the <see cref="Command.Changed"/> event.
		/// </summary>
		/// <param name="command">
		/// The <see cref="Command"/> that changed.
		/// </param>
		protected override void OnCommandChanged(Command command)
		{
			base.OnCommandChanged(command);

			foreach (UIElement key in Invokers.Keys)
			{
				key.IsEnabled = (command.Status == CommandStatus.Enabled);
				key.Visibility = (command.Status == CommandStatus.Unavailable) ? Visibility.Collapsed : Visibility.Visible;
			}
		}
	}
}